package com.wzy.new_energy_car_platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzy.new_energy_car_platform.common.GlobalVariable;
import com.wzy.new_energy_car_platform.common.ResultDto;
import com.wzy.new_energy_car_platform.dao.CarMapper;
import com.wzy.new_energy_car_platform.dao.UserCollectMapper;
import com.wzy.new_energy_car_platform.model.Car;
import com.wzy.new_energy_car_platform.model.SysUser;
import com.wzy.new_energy_car_platform.model.UserCollect;
import com.wzy.new_energy_car_platform.service.SysUserService;
import com.wzy.new_energy_car_platform.service.UserCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.new_energy_car_platform.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 用户收藏表 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2024-04-10
 */
@Service
public class UserCollectServiceImpl extends ServiceImpl<UserCollectMapper, UserCollect> implements UserCollectService {
    @Autowired
    private UserCollectMapper userCollectMapper;

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public ResultDto<?> getMyCollect(int userId) {
        LambdaQueryWrapper<UserCollect> userCollectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userCollectLambdaQueryWrapper.eq(UserCollect::getUserId,userId)
                .eq(UserCollect::getValidFlag, GlobalVariable.VALID_TRUE)
                .orderByDesc(UserCollect::getId)
                .last("limit 4");
        List<UserCollect> userCollects = userCollectMapper.selectList(userCollectLambdaQueryWrapper);
        List<Map> result = new ArrayList<>();
        if (Objects.nonNull(userCollects)){
            for (UserCollect userCollect : userCollects) {
                Map map = new HashMap();
                Long carId = userCollect.getCarId();
                Car car = carMapper.selectById(carId);
                map.put("name",car.getName());
                map.put("id",car.getId());
                map.put("img",car.getImg());
                map.put("price",car.getMinPrice()+"-"+car.getMaxPrice());
                map.put("salesNum",car.getSalesNum());
                result.add(map);

            }
        }

        return ResultDto.ok(result);
    }

    @Override
    public ResultDto<?> addCollect(Map map) {
        if (Objects.nonNull(map)){
            String token = map.get("token").toString();
            SysUser sysUser = (SysUser) sysUserService.getUser(token).getData();
            Long userId = sysUser.getId();
            Long carId = Long.parseLong(map.get("carId").toString());
            LambdaQueryWrapper<UserCollect> userCollectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userCollectLambdaQueryWrapper.eq(UserCollect::getUserId,userId)
                    .eq(UserCollect::getCarId,carId)
                    .eq(UserCollect::getValidFlag,GlobalVariable.VALID_TRUE);
            UserCollect userCollect = userCollectMapper.selectOne(userCollectLambdaQueryWrapper);
            if (Objects.nonNull(userCollect)){
                return null;
            }
            userCollect = new UserCollect();
            userCollect.setUserId(userId);
            userCollect.setCarId(carId);
            userCollectMapper.insert(userCollect);
            return ResultDto.ok();
        }
        return null;
    }

    @Override
    public ResultDto<?> cancelCollect(Map map) {
        if (Objects.nonNull(map)){
            String token = map.get("token").toString();
            SysUser sysUser = (SysUser) sysUserService.getUser(token).getData();
            Long userId = sysUser.getId();
            Long carId = Long.parseLong(map.get("carId").toString());
            LambdaQueryWrapper<UserCollect> userCollectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userCollectLambdaQueryWrapper.eq(UserCollect::getUserId,userId)
                    .eq(UserCollect::getCarId,carId)
                    .eq(UserCollect::getValidFlag,GlobalVariable.VALID_TRUE);
            UserCollect userCollect = userCollectMapper.selectOne(userCollectLambdaQueryWrapper);
            if (Objects.nonNull(userCollect)){
                userCollect.setValidFlag(GlobalVariable.VALID_FALSE);
                userCollectMapper.updateById(userCollect);
            }
        }
        return null;
    }
}
